package cn.com.b2office.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import cn.com.b2office.dao.BrandDao;
import cn.com.b2office.dao.ItemDao;
import cn.com.b2office.domain.Item;
import cn.com.b2office.domain.ItemBrand;
import cn.com.b2office.service.ItemBrandService;

public class ItemBrandServiceImpl implements ItemBrandService {

	private BrandDao brandDao = null;

	private ItemDao itemDao = null;

	public void setBrandDao(BrandDao brandDao) {
		this.brandDao = brandDao;
	}

	public void setItemDao(ItemDao itemDao) {
		this.itemDao = itemDao;
	}

	public ItemBrand getItemBrand(String brandId) {
		return brandDao.getBrand(brandId);
	}

	/** 查询某一类别下的所有品牌 */
	public List<ItemBrand> findItemBrands(String categoryId) {
		// TODO: 需优化
		List<Item> itemList = itemDao.findItemsByCategory(categoryId);
		Set<String> brandIdSet = new HashSet<String>();
		for (Item item : itemList) {
			brandIdSet.add(item.getItem_brand());
		}
		if (brandIdSet.size() == 0)
			return new ArrayList<ItemBrand>();
		return brandDao.findBrands(brandIdSet.toArray(new String[0]));
	}

}
